<?php
if (!defined('IN_MEDIA')) die("Hacking attempt");
class mysql { 
	var $link_id;
	var $log_file = 'logs.txt';
	var $log_error = 1;
	function connect($db_host, $db_username, $db_password, $db_name) {
		$this->link_id = @mysql_connect($db_host, $db_username, $db_password);
		if ($this->link_id)
		{
			if (@mysql_select_db($db_name, $this->link_id)) return $this->link_id;
			else $this->show_error('Unable to select database. MySQL reported: '.mysql_error());
		}
		else $this->show_error('Unable to connect to MySQL server. MySQL reported: '.mysql_error());
	}
	
	function query($input){
		$q = @mysql_query($input) or $this->show_error("<b>Lỗi MySQL Query</b> : ".mysql_error(),$input);
		return $q;
	}
	
	function fetch_array($query_id, $type=MYSQL_BOTH){
		$fa = @mysql_fetch_array($query_id,$type);
		return $fa;
	}
	
	function num_rows($query_id) {
		$nr = @mysql_num_rows($query_id);
		return $nr;
	}
	
	function result($query_id, $row=0, $field) {
		$r = @mysql_result($query_id, $row, $field);
		return $r;
	}
	
	function insert_id() {
		return @mysql_insert_id($this->link_id);
	}
	function show_error($input,$q){
		if ($this->log_error) {
			$file_name = $this->log_file;
			$fp = fopen($file_name,'a');
			flock($fp,2);
			fwrite($fp,"### ".date('H:s:i d-m-Y',NOW)." ###\n");
			fwrite($fp,$input."\n");
			fwrite($fp,"QUERY : ".$q."\n");
			flock($fp,1);
			fclose($fp);
		}
		die($input);
	}
		function paginate($numRows, $maxRows, $pageNum=1, $pageVar="page", $class="pagelink"){
                $navigation = "";
                // Lay total pages
                $totalPages = ceil($numRows/$maxRows);
                // develop query string minus page vars
                $queryString = "";
                if (!empty($_SERVER['QUERY_STRING'])) {
                    $params = explode("&", $_SERVER['QUERY_STRING']);
                    $newParams = array();
                    foreach ($params as $param) {
                             if (stristr($param, $pageVar) == false) {
                                 array_push($newParams, $param);
                             }
                    }
                    if (count($newParams) != 0) {
                        $queryString = "&" . htmlentities(implode("&", $newParams));
                    }
                }
                // get current page
                $currentPage = $PHP_SELF;
                //print "currentPage = ".$currentPage."<br>";
                // build page navigation
                if ($totalPages> 1){
                    $navigation = "<span class=\"pagecur\">".$totalPages." Trang</span>";
                    $upper_limit = $pageNum + 10;
                    $lower_limit = $pageNum - 2;
                    if ($pageNum > 1) { // Show if not first page
                        if(($pageNum - 2)>0){
                            $first = sprintf("%s". $pageVar."-%d%s.html", $currentPage, 1, $queryString);
                            $navigation .= "&nbsp;<a href='".$first."' onfocus=this.blur() class='".$class."'><font color=\"red\">First</font></a>";}
                            $prev = sprintf("%s". $pageVar."-%d%s.html", $currentPage, max(0, $pageNum - 1), $queryString);
                            $navigation .= "&nbsp;<a href='".$prev."' onfocus=this.blur() class='".$class."'><font color=\"red\">&lt;</font></a>";
                        } // Show if not first page
                        // get in between pages
                        for($i = 1; $i < $totalPages+1; $i++){
                            $pageNo = $i;
                            if($i==$pageNum){
                               $navigation .= "&nbsp;<a onfocus=this.blur() class=pagecurrent>".$pageNo."</a>";
                            } elseif($i!==$pageNum && $i<$upper_limit && $i>$lower_limit){
                               $noLink = sprintf("%s".$pageVar."-%d%s.html",  $currentPage,  $i , $queryString);
                               $navigation .= "&nbsp;<a href='".$noLink."' onfocus=this.blur() class='".$class."'>".$pageNo."</a>";
                               } elseif(($i - $lower_limit)==0){
                                         $navigation .=  "&hellip;";
                               }
                        }
                        if (($pageNum+1) < $totalPages) { // Show if not last page
                             $next = sprintf("%s".$pageVar."-%d%s.html", $currentPage, min($totalPages, $pageNum + 1), $queryString);
                             $navigation .= "&nbsp;<a href='".$next."' onfocus=this.blur() class='".$class."'><font color=\"red\">&gt;</font></a> ";
                             if(($pageNum + 3)<$totalPages){
                                 $last = sprintf("%s".$pageVar."-%d%s.html", $currentPage, $totalPages, $queryString);
                                 $navigation .= "&nbsp;<a href='".$last."' onfocus=this.blur() class='".$class."'><font color=\"red\">Last</font></a>";
                             }
                        } // Show if not last page
                 } // end if total pages is greater than one
                 return $navigation;
       }
	   	function rewrite_page($numRows, $maxRows, $pageNum=1, $pageVar="page", $class="pagelink"){
                $navigation = "";
                // Lay total pages
                $totalPages = ceil($numRows/$maxRows);
                // develop query string minus page vars
                $queryString = "";
                if (!empty($_SERVER['QUERY_STRING'])) {
                    $params = explode("&", $_SERVER['QUERY_STRING']);
                    $newParams = array();
                    foreach ($params as $param) {
                             if (stristr($param, $pageVar) == false) {
                                 array_push($newParams, $param);
                             }
                    }
                    if (count($newParams) != 0) {
                        $queryString = "&" . htmlentities(implode("&", $newParams));
                    }
                }
                // get current page
                $currentPage = $PHP_SELF;
                //print "currentPage = ".$currentPage."<br>";
                // build page navigation
                if ($totalPages> 1){
                    $navigation = "<span class=\"pagecur\">".$totalPages." Trang</span>";
                    $upper_limit = $pageNum + 10;
                    $lower_limit = $pageNum - 2;
                    if ($pageNum > 1) { // Show if not first page
                        if(($pageNum - 2)>0){
                            $first = sprintf("%s?". $pageVar."=%d%s", $currentPage, 1, $queryString);
                            $navigation .= "&nbsp;<a href='".$first."' onfocus=this.blur() class='".$class."'><font color=\"red\">First</font></a>";}
                            $prev = sprintf("%s?". $pageVar."=%d%s", $currentPage, max(0, $pageNum - 1), $queryString);
                            $navigation .= "&nbsp;<a href='".$prev."' onfocus=this.blur() class='".$class."'><font color=\"red\">&lt;</font></a>";
                        } // Show if not first page
                        // get in between pages
                        for($i = 1; $i < $totalPages+1; $i++){
                            $pageNo = $i;
                            if($i==$pageNum){
                               $navigation .= "&nbsp;<a onfocus=this.blur() class=pagecurrent>".$pageNo."</a>";
                            } elseif($i!==$pageNum && $i<$upper_limit && $i>$lower_limit){
                               $noLink = sprintf("%s?".$pageVar."=%d%s",  $currentPage,  $i , $queryString);
                               $navigation .= "&nbsp;<a href='".$noLink."' onfocus=this.blur() class='".$class."'>".$pageNo."</a>";
                               } elseif(($i - $lower_limit)==0){
                                         $navigation .=  "&hellip;";
                               }
                        }
                        if (($pageNum+1) < $totalPages) { // Show if not last page
                             $next = sprintf("%s?".$pageVar."=%d%s", $currentPage, min($totalPages, $pageNum + 1), $queryString);
                             $navigation .= "&nbsp;<a href='".$next."' onfocus=this.blur() class='".$class."'><font color=\"red\">&gt;</font></a> ";
                             if(($pageNum + 3)<$totalPages){
                                 $last = sprintf("%s?".$pageVar."=%d%s", $currentPage, $totalPages, $queryString);
                                 $navigation .= "&nbsp;<a href='".$last."' onfocus=this.blur() class='".$class."'><font color=\"red\">Last</font></a>";
                             }
                        } // Show if not last page
                 } // end if total pages is greater than one
                 return $navigation;
       }
}
?>